package com.itheima.controller.product;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.Product;
import com.itheima.utils.BeanUtil;
import com.itheima.utils.UidUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/product/*")
public class ProductServlet extends BaseServlet {

    /**
     * 展示产品列表
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Page<Product> page = null;

        //获取页码和每页显示最大条数
        String pageNum=req.getParameter("pageNum");
        String pageSize=req.getParameter("pageSize");
        String name = req.getParameter("name");

        if (name != null){
            page = productService.findAllByName(pageNum,pageSize,name);
        }else{
            page = productService.findAll(pageNum,pageSize);
        }
        //业务层对象 调用方法 得到请求回复内容

        //将数据交由分页插件处理获取分页信息
        PageInfo<Product> pageInfo=new PageInfo<>(page);
        //存入作用域
        req.setAttribute("page",pageInfo);
        //将得到的list数据响应给前端主界面
        req.getRequestDispatcher("/pages/product/product-list.jsp").forward(req,resp);
    }

    /**
     * 新增 产品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取前端传入的数据,调用工具类封装成product对象
        Product product = BeanUtil.fillBean(req, Product.class, "yyyy-MM-dd HH:mm");
        //设置新增产品的id
        product.setId( UidUtil.getUid());
        //调用业务层方法将对象放入数据库中
        productService.save(product);
        //再次遍历所有产品数据
        list(req,resp);

    }


    /**
     * 根据id回显要修改的原始数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收前端传入的数据
        String id = req.getParameter("id");
        //调用业务层方法 根据id返回product对象
        Product product = productService.findById(id);
        //将产品数据传入域中 回显到修改页面
        req.setAttribute("product",product);
        //跳转到修改页面
        req.getRequestDispatcher("/pages/product/product-update.jsp").forward(req,resp);
    }

    /**
     * 修改数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取前端传入的数据,调用工具类封装成product对象
        Product product = BeanUtil.fillBean(req, Product.class, "yyyy-MM-dd HH:mm" +
                "");
        System.out.println("product = -------------------------" + product);
        //调用业务层方法将对象放入数据库中
        productService.update(product);
        //再次遍历所有产品数据


        list(req,resp);

    }

    /**
     * 根据ids批量删除产品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传入的ids数据
        String ids = req.getParameter("ids");
        //调用业务层方法 根据ids删除数据 返回boolean值
        Boolean flag = productService.deleteByIds(ids);
        String json = mapper.writeValueAsString(flag);
        resp.getWriter().write(json);
    }

    /**
     * 通过ids打开状态
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void openByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传入的ids数据
        String ids = req.getParameter("ids");
        //调用业务层方法 根据ids删除数据 返回boolean值
        productService.openByIds(ids);
        resp.getWriter().write("成功");
    }

    /**
     * 通过ids关闭状态
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void closeByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传入的ids数据
        String ids = req.getParameter("ids");
        //调用业务层方法 根据ids删除数据 返回boolean值
        productService.closeByIds(ids);
        resp.getWriter().write("成功");
    }
}

